home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
STANDALO
/
RESMAP-F
/
RESMAP_F.C
next >
Wrap
C/C++ Source or Header
|
1991-01-27
|
8KB
|
340 lines
/*
ResMap FKEY by James W. Walker
This FKEY displays a list of the open resource files,
in the order in which the Resource Manager searches them.
This is written for THINK C 4.0. Note that "Custom Headers"
should be turn on in the "Set Project Type..." dialog.
*/
#define DEBUGGING 0 /* Compile in DebugStr instructions? */
#if DEBUGGING
#define DEBUGSTR(s) DebugStr(s)
#else
#define DEBUGSTR(s) SysBeep(1)
#endif
#include <HFS.h>
#include <FileMgr.h>
#include <MultiFinder.h>
#include <MacProto.h>
#define nil 0L
#define MOVE_TO_FRONT -1L
#define BUTTON_WIDTH 60
#define BUTTON_HEIGHT 20
#define BUTTON_GAP 10 /* Vertical distance from button */
/* to other stuff. */
#define BOX_HEIGHT 250
#define BOX_WIDTH 240
#define OSDISPATCH 0xA88F
#define UNIMPLEMENTED 0xA89F
typedef struct Resource_map {
long rhead[4];
struct Resource_map **next; /* Handle to next resource map. */
short refnum; /* resource file reference number */
/* ...and other stuff I don't need to reference. */
} **ResMap_h;
typedef struct QuickDraw {
long randSeed;
BitMap screenBits;
Cursor arrow;
Pattern dkGray;
Pattern ltgray;
Pattern gray;
Pattern black;
Pattern white;
GrafPtr thePort;
} QuickDraw;
/* --------------- Prototypes --------------------- */
Boolean Check_handle( Handle h, Ptr top_mem );
void main( void );
/* ------------------- main ----------------------- */
void main()
{
register WindowPtr my_window;
WindowPtr which_window;
Rect bounds, listview, databounds, listbox, button_rect;
GrafPtr save_port;
register ResMap_h this_map;
FCBPBRec fcb;
Str255 filename;
ListHandle list;
Cell ncell; /* name */
EventRecord event;
Boolean done;
short place, part;
ControlHandle button, which_control;
int cur_resfile = CurResFile();
register short row_num;
Ptr top_mem;
QuickDraw *QD_globals;
static Boolean running = false; /* Prevent reentrance */
asm {
movem.l a0-a5/d0-d7, -(SP) ; save registers
LEA main, A4 ; for addressing the static variable
}
if (running)
{
SysBeep(1);
goto reentrant_exit;
}
running = true;
GetPort( &save_port );
/* Find the top of RAM, for checking handles. */
if (NGetTrapAddress(OSDISPATCH, ToolTrap) !=
NGetTrapAddress(UNIMPLEMENTED, ToolTrap))
top_mem = StripAddress(MFTopMem());
else
top_mem = StripAddress(TopMem());
/*
Get QuickDraw globals, so we can find the screen size.
*/
QD_globals = (QuickDraw *) ( *(Byte **) CurrentA5 -
(sizeof(QuickDraw) - sizeof(GrafPtr)) );
/*
Create a window (centered) and set its font.
*/
bounds.top = QD_globals->screenBits.bounds.top + GetMBarHeight() +
(QD_globals->screenBits.bounds.bottom -
QD_globals->screenBits.bounds.top - GetMBarHeight()
- BOX_HEIGHT) / 2;
bounds.bottom = bounds.top + BOX_HEIGHT;
bounds.left = QD_globals->screenBits.bounds.left +
(QD_globals->screenBits.bounds.right -
QD_globals->screenBits.bounds.left - BOX_WIDTH) / 2;
bounds.right = bounds.left + BOX_WIDTH;
my_window = NewWindow( nil, &bounds, "\pFKEY", true, dBoxProc,
(WindowPtr) MOVE_TO_FRONT, true, nil );
/*
It would seem reasonable to use a null string as the window
title and use false as the goAwayFlag parameter, since the
window has no title bar. Then I noticed that the "Select
rear window" QuicKey (in QuicKeys 2.0.1) would send the
FKEY's window behind other windows, but would not bring it back.
Changing these parameters fixes this.
*/
if (my_window == nil)
{
DEBUGSTR("\pResmap can't create window.");
goto getout;
}
((WindowPeek)my_window)->windowKind = 1;
SetPort( my_window );
TextSize( 12 );
TextFont( geneva );
/*
Frame the rectangle in which we'll display the scrolling list.
*/
listbox = my_window->portRect;
InsetRect( &listbox, 3, 3 );
/* Leave room for the Cancel button */
listbox.bottom -= BUTTON_HEIGHT + 2*BUTTON_GAP;
FrameRect( &listbox );
/*
Create the Cancel button.
*/
SetRect( &button_rect,
(listbox.right + listbox.left - BUTTON_WIDTH) / 2,
listbox.bottom + BUTTON_GAP,
(listbox.right + listbox.left + BUTTON_WIDTH) / 2,
listbox.bottom + BUTTON_GAP + BUTTON_HEIGHT );
button = NewControl( my_window, &button_rect, "\pCancel", true,
0, 0, 1, pushButProc, nil );
if (button == nil)
{
DEBUGSTR("\pCouldn't create a button");
SetPort( save_port );
DisposeWindow( my_window );
goto getout;
}
/* Create the list. */
listview = listbox;
InsetRect( &listview, 1, 1 );
listview.right -= 15; /* Don't include the scroll bar. */
SetRect( &databounds, 0, 0, 1, 0 ); /* 1 column, │ 0 rows */
ncell.h = ncell.v = 0;
list = LNew( &listview,
&databounds, /* Preallocated storage */
ncell, /* Cell size: use default. */
0, /* default LDEF */
my_window,
false, /* Don't draw yet. */
false, /* No grow box. */
false, /* No horizontal scrolling. */
true /* We do vertical scrolling. */
);
if (!Check_handle( (Handle) list, top_mem ))
{
DEBUGSTR("\pBad list handle!");
DisposeControl( button );
SetPort( save_port );
DisposeWindow( my_window );
goto getout;
}
/* Fill the list. */
ncell.h = ncell.v =0; /* ncell will always be in column 0 */
row_num = 0;
fcb.ioCompletion = nil;
fcb.ioFCBIndx = nil;
this_map = (ResMap_h) TopMapHndl;
while ( Check_handle( (Handle) this_map, top_mem ) )
{
ncell.v = row_num;
(void) LAddRow( 1, row_num, list );
fcb.ioRefNum = (**this_map).refnum;
fcb.ioVRefNum = 0;
fcb.ioNamePtr = (StringPtr) filename;
PBGetFCBInfo( &fcb, false );
if ( fcb.ioResult == noErr )
LSetCell( &filename[1], (int) filename[0], ncell, list );
else
LSetCell( "***** Can't get file info *****", 31, ncell, list );
if ( (**this_map).refnum == cur_resfile )
LSetSelect( true, ncell, list );
this_map = (**this_map).next;
++row_num;
}
ncell.v = row_num;
(void) LAddRow( 6, ncell.v, list );
LSetCell( "-------------------------------", 31, ncell, list );
++ncell.v;
LSetCell( "This FKEY shows the list of", 27, ncell, list );
++ncell.v;
LSetCell( "resource maps. The CurResFile", 29, ncell, list );
++ncell.v;
LSetCell( "is highlighted. Vers. 1.3.", 27, ncell, list );
++ncell.v;
LSetCell( "Freeware by Jim Walker, 1991.", 29, ncell, list );
++ncell.v;
LSetCell( "76367.2271@compuserve.com", 25, ncell, list );
LDoDraw( true, list );
/*
Process update, mousedown, and keydown events. Quit when we
see a click on the Cancel button, or any keydown.
*/
done = false;
while (!done)
{
(void)GetNextEvent( everyEvent, &event );
SetPort( my_window ); /* Probably not needed. */
switch (event.what)
{
case mouseDown:
place = FindWindow( event.where, &which_window );
if ( (which_window == my_window) && (place == inContent) )
{
GlobalToLocal( &event.where );
part = FindControl( event.where, my_window,
&which_control );
if (which_control == button)
{
part = TrackControl( button, event.where, nil );
if (part == inButton)
done = true;
}
else if (which_control != nil) /* scroll bar */
LClick( event.where, event.modifiers, list );
}
else
SysBeep(1);
break;
case updateEvt:
if ((WindowPtr)event.message == my_window)
{
BeginUpdate( my_window );
LUpdate( my_window->visRgn, list );
FrameRect( &listbox );
DrawControls( my_window );
EndUpdate( my_window );
}
break;
case keyDown:
done = true;
break;
}
}
LDispose( list );
DisposeControl( button );
SetPort( save_port );
DisposeWindow( my_window );
getout:
running = false;
reentrant_exit:
asm {
movem.l (SP)+, a0-a5/d0-d7
}
}
/* --------------------------- Check_handle --------------------------- */
Boolean Check_handle( Handle h, Ptr top_mem )
{
THz zone;
if (h == nil)
return( false );
if ((long)h & 1L)
{
DEBUGSTR("\pOdd handle");
return( false );
}
if (StripAddress((Ptr)h) > top_mem)
{
DEBUGSTR("\pHigh handle");
return( false );
}
if (*h == nil)
{
DEBUGSTR("\pEmpty handle");
return( false );
}
if (StripAddress((Ptr)*h) > top_mem)
{
DEBUGSTR("\pHigh master pointer");
return( false );
}
if ((long)*h & 1L)
{
DEBUGSTR("\pOdd master pointer");
return( false );
}
zone = HandleZone( h );
if (MemError() != noErr)
{
DEBUGSTR("\pAttempt to operate on a free block?");
return( false );
}
return( true );
}